একটি বাফার ব্লক কি?
Node.js Buffer .
বাফারগুলি পূর্ণসংখ্যার অ্যারের মতো, তবে নির্দিষ্ট দৈর্ঘ্যের এবং V8 জাভাস্ক্রিপ্ট ইঞ্জিনের বাইরে কাঁচা মেমরি বরাদ্দের সাথে মিলে যায়।
Node.js Buffer , .
দ্রষ্টব্য:
Node.js v6.0.0 , Buffer Buffer .
কনস্ট্রাক্টর ব্যবহার করা অপ্রচলিত মেমরির কারণে নিরাপত্তা দুর্বলতা হতে পারে।
Buffers সঙ্গে শুরু করা
Node.js Buffers .
এগুলি পূর্ণসংখ্যার অ্যারের মতো, তবে আকারে স্থির এবং V8 হিপের বাইরে কাঁচা মেমরি বরাদ্দের প্রতিনিধিত্ব করে।
বেসিক বাফার উদাহরণ
// Create a buffer from a string
const buf = Buffer.from('Hello, Node.js!');
// Buffers can be converted to strings
console.log(buf.toString()); // 'Hello, Node.js!'
// Access individual bytes
console.log(buf[0]); // 72 (ASCII for 'H')
// Buffers have a fixed length
console.log(buf.length); // 15
বাফার তৈরি করা
Node.js buffers , :
1. Buffer.alloc()
নির্দিষ্ট আকারের একটি নতুন বাফার তৈরি করে, শূন্য দিয়ে শুরু করা হয়।
এটি একটি নতুন বাফার তৈরি করার সবচেয়ে নিরাপদ উপায় কারণ এটি নিশ্চিত করে যে কোনও পুরানো ডেটা উপস্থিত নেই৷
// Create a buffer of 10 bytes filled with zeros
const buffer1 = Buffer.alloc(10);
console.log(buffer1);
2. Buffer.allocUnsafe()
নির্দিষ্ট আকারের একটি নতুন বাফার তৈরি করে, কিন্তু মেমরি আরম্ভ করে না।
এটি Buffer.alloc() এর চেয়ে দ্রুত, তবে পুরানো বা সংবেদনশীল ডেটা থাকতে পারে।
যদি নিরাপত্তা একটি উদ্বেগ হয়, সর্বদা ব্যবহারের আগে বাফার পূরণ করুন.
// Create an uninitialized buffer of 10 bytes
const buffer2 = Buffer.allocUnsafe(10);
console.log(buffer2);
// Fill the buffer with zeros for security
buffer2.fill(0);
console.log(buffer2);
সতর্কতা:
Buffer.allocUnsafe() Buffer.alloc() , .
আপনি নিরাপত্তার প্রভাব বুঝতে পারলে এবং অবিলম্বে সম্পূর্ণ বাফার পূরণ করার পরিকল্পনা করলেই এটি ব্যবহার করুন৷
3. Buffer.from()
স্ট্রিংস, অ্যারে বা অ্যারেবাফারের মতো বিভিন্ন উত্স থেকে একটি নতুন বাফার তৈরি করে। বিদ্যমান ডেটা থেকে বাফার তৈরি করার এটি একটি খুব নমনীয় উপায়।
// Create a buffer from a string
const buffer3 = Buffer.from('Hello, World!');
console.log(buffer3);
console.log(buffer3.toString());
// Create a buffer from an array of integers
const buffer4 = Buffer.from([65, 66, 67, 68, 69]);
console.log(buffer4);
console.log(buffer4.toString());
// Create a buffer from another buffer
const buffer5 = Buffer.from(buffer4);
console.log(buffer5);
বাফার ব্যবহার করে
বাফারে লেখা
বিভিন্ন পদ্ধতি ব্যবহার করে একটি বাফারে ডেটা লেখা যেতে পারে:
// Create an empty buffer
const buffer = Buffer.alloc(10);
// Write a string to the buffer
buffer.write('Hello');
console.log(buffer);
console.log(buffer.toString());
// Write bytes at specific positions
buffer[5] = 44; // ASCII for ','
buffer[6] = 32; // ASCII for space
buffer.write('Node', 7);
console.log(buffer.toString());
Buffers থেকে পড়া
বিভিন্ন পদ্ধতি ব্যবহার করে একটি বাফার থেকে ডেটা পড়া যেতে পারে:
// Create a buffer from a string
const buffer = Buffer.from('Hello, Node.js!');
// Read the entire buffer as a string
console.log(buffer.toString());
// Read a portion of the buffer (start at position 7, end before position 11)
console.log(buffer.toString('utf8', 7, 11));
// Read a single byte
console.log(buffer[0]);
// Convert the ASCII code to a character
console.log(String.fromCharCode(buffer[0]));
বাফার মাধ্যমে পুনরাবৃত্তি
বাফারগুলি অ্যারের মতো পুনরাবৃত্তি করা যেতে পারে:
// Create a buffer from a string
const buffer = Buffer.from('Hello');
// Iterate using for...of loop
for (const byte of buffer) {
console.log(byte);
}
// Iterate using forEach
buffer.forEach((byte, index) => {
console.log(`Byte at position ${index}: ${byte}`);
});
বাফার পদ্ধতি
Buffer.compare()
দুটি বাফারের তুলনা করে এবং একটি সংখ্যা প্রদান করে যা নির্দেশ করে যে প্রথমটি দ্বিতীয়টির আগে, অনুসরণ করে বা দ্বিতীয়টির অনুরূপ:
const buffer1 = Buffer.from('ABC');
const buffer2 = Buffer.from('BCD');
const buffer3 = Buffer.from('ABC');
console.log(Buffer.compare(buffer1, buffer2));
console.log(Buffer.compare(buffer2, buffer1));
console.log(Buffer.compare(buffer1, buffer3));
buffer.copy()
এক বাফার থেকে অন্য বাফারে ডেটা অনুলিপি করা:
// Create source and target buffers
const source = Buffer.from('Hello, World!');
const target = Buffer.alloc(source.length);
// Copy from source to target
source.copy(target);
console.log(target.toString());
// Create a target buffer for partial copy
const partialTarget = Buffer.alloc(5);
// Copy only part of the source (starting at index 7)
source.copy(partialTarget, 0, 7);
console.log(partialTarget.toString());
buffer.slice()
একটি নতুন বাফার তৈরি করে যা মূলের মতো একই মেমরির দিকে নির্দেশ করে, কিন্তু প্রদত্ত ফলাফলে অফসেট এবং ছাঁটাই করে:
const buffer = Buffer.from('Hello, World!');
// Create a slice from position 7 to the end
const slice = buffer.slice(7);
console.log(slice.toString());
// Create a slice from position 0 to 5
const slice2 = buffer.slice(0, 5);
console.log(slice2.toString());
// Important: slices share memory with original buffer
slice[0] = 119; // ASCII for 'w' (lowercase)
console.log(slice.toString());
console.log(buffer.toString());
দ্রষ্টব্য:
buffer.slice() , buffer slice .
buffer.toString()
নির্দিষ্ট এনকোডিং ব্যবহার করে একটি স্ট্রিং এ একটি বাফার ডিকোড করে:
const buffer = Buffer.from('Hello, World!');
// Default encoding is UTF-8
console.log(buffer.toString());
// Specify encoding
console.log(buffer.toString('utf8'));
// Decode only a portion of the buffer
console.log(buffer.toString('utf8', 0, 5));
// Using different encodings
const hexBuffer = Buffer.from('48656c6c6f', 'hex');
console.log(hexBuffer.toString());
const base64Buffer = Buffer.from('SGVsbG8=', 'base64');
console.log(base64Buffer.toString());
buffer.equals()
বিষয়বস্তু সমতার জন্য দুটি বাফার তুলনা করুন:
const buffer1 = Buffer.from('Hello');
const buffer2 = Buffer.from('Hello');
const buffer3 = Buffer.from('World');
console.log(buffer1.equals(buffer2));
console.log(buffer1.equals(buffer3));
console.log(buffer1 === buffer2);
এনক্রিপশন নিয়ে কাজ করা
স্ট্রিং এবং বাইনারি ডেটার মধ্যে রূপান্তর করার সময় বাফারগুলি বিভিন্ন এনকোডিংয়ের সাথে কাজ করে:
// Create a string
const str = 'Hello, World!';
// Convert to different encodings
const utf8Buffer = Buffer.from(str, 'utf8');
console.log('UTF-8:', utf8Buffer);
const base64Str = utf8Buffer.toString('base64');
console.log('Base64 string:', base64Str);
const hexStr = utf8Buffer.toString('hex');
console.log('Hex string:', hexStr);
// Convert back to original
const fromBase64 = Buffer.from(base64Str, 'base64').toString('utf8');
console.log('From Base64:', fromBase64);
const fromHex = Buffer.from(hexStr, 'hex').toString('utf8');
console.log('From Hex:', fromHex);
Node.js-এ সমর্থিত এনকোডিং:
| এনক্রিপশন | ব্যাখ্যা |
|---|---|
| utf8 | মাল্টি-বাইট এনকোডেড ইউনিকোড অক্ষর (ডিফল্ট) |
| ascii | শুধুমাত্র ASCII অক্ষর (7-বিট) |
| latin1 | ল্যাটিন-1 কোডিং (ISO 8859-1) |
| base64 | বেস 64 এনকোডিং |
| hex | হেক্সাডেসিমেল এনকোডিং |
| binary | বাইনারি এনকোডিং (পরিত্যক্ত) |
| ucs2/utf16le | 2 বা 4 বাইট, লিটল-এন্ডিয়ান এনকোডেড ইউনিকোড অক্ষর |
উন্নত বাফার ফাংশন
লিঙ্কিং বাফার
Buffer.concat() buffers :
const buf1 = Buffer.from('Hello, ');
const buf2 = Buffer.from('Node.js!');
// Concatenate buffers
const combined = Buffer.concat([buf1, buf2]);
console.log(combined.toString()); // 'Hello, Node.js!'
// With a maximum length parameter
const partial = Buffer.concat([buf1, buf2], 5);
console.log(partial.toString()); // 'Hello'
বাফারে অনুসন্ধান করা হচ্ছে
বাফারগুলি মান বা অ্যারে অনুসন্ধানের জন্য পদ্ধতি সরবরাহ করে:
const buf = Buffer.from('Hello, Node.js is awesome!');
// Find the first occurrence of a value
console.log(buf.indexOf('Node')); // 7
// Check if buffer contains a value
console.log(buf.includes('awesome')); // true
// Find the last occurrence of a value
console.log(buf.lastIndexOf('e')); // 24
বাফার এবং স্ট্রীম
বাফারগুলি সাধারণত খোলা ডেটা প্রক্রিয়াকরণের জন্য স্ট্রিমগুলির সাথে ব্যবহৃত হয়:
const fs = require('fs');
const { Transform } = require('stream');
// Create a transform stream that processes data in chunks
const transformStream = new Transform({
transform(chunk, encoding, callback) {
// Process each chunk (which is a Buffer)
const processed = chunk.toString().toUpperCase();
this.push(Buffer.from(processed));
callback();
}
});
// Create a read stream from a file
const readStream = fs.createReadStream('input.txt');
// Create a write stream to a file
const writeStream = fs.createWriteStream('output.txt');
// Process the file in chunks
readStream.pipe(transformStream).pipe(writeStream);
বাফার এবং ফাইল সিস্টেম
বাফারগুলি সাধারণত ফাইল সিস্টেম অপারেশনের জন্য ব্যবহৃত হয়:
const fs = require('fs');
// Write buffer to file
const writeBuffer = Buffer.from('Hello, Node.js!');
fs.writeFile('buffer.txt', writeBuffer, (err) => {
if (err) throw err;
console.log('File written successfully');
// Read file into buffer
fs.readFile('buffer.txt', (err, data) => {
if (err) throw err;
// 'data' is a buffer
console.log('Read buffer:', data);
console.log('Buffer content:', data.toString());
// Read only part of the file into a buffer
const smallBuffer = Buffer.alloc(5);
fs.open('buffer.txt', 'r', (err, fd) => {
if (err) throw err;
// Read 5 bytes starting at position 7
fs.read(fd, smallBuffer, 0, 5, 7, (err, bytesRead, buffer) => {
if (err) throw err;
console.log('Partial read:', buffer.toString());
// Output: Node.
fs.close(fd, (err) => {
if (err) throw err;
});
});
});
});
});
বাফার কর্মক্ষমতা বিবেচনা
বাফার পুল প্রক্রিয়াকরণ
// Simple buffer pool implementation
class BufferPool {
constructor(bufferSize = 1024, poolSize = 10) {
this.bufferSize = bufferSize;
this.pool = Array(poolSize).fill().map(() => Buffer.alloc(bufferSize));
this.used = Array(poolSize).fill(false);
}
// Get a buffer from the pool
get() {
const index = this.used.indexOf(false);
if (index === -1) {
// Pool is full, create a new buffer
console.log('Pool full, allocating new buffer');
return Buffer.alloc(this.bufferSize);
}
this.used[index] = true;
return this.pool[index];
}
// Return a buffer to the pool
release(buffer) {
const index = this.pool.indexOf(buffer);
if (index !== -1) {
// Zero the buffer for security
buffer.fill(0);
this.used[index] = false;
}
}
}
// Usage example
const pool = new BufferPool(10, 3); // 3 buffers of 10 bytes each
const buf1 = pool.get();
const buf2 = pool.get();
const buf3 = pool.get();
const buf4 = pool.get(); // This will allocate a new buffer
buf1.write('Hello');
console.log(buf1.toString()); // Hello
// Return buf1 to the pool
pool.release(buf1);
// Get another buffer (should reuse buf1)
const buf5 = pool.get();
console.log(buf5.toString()); // Should be empty (zeros)
বাফার নিরাপত্তা বিবেচনা
নিরাপত্তা সতর্কতা:
বাফারে মেমরি থেকে সংবেদনশীল ডেটা থাকতে পারে।
বাফারগুলি পরিচালনা করার সময় সর্বদা সতর্ক থাকুন, বিশেষত সেগুলি ব্যবহারকারীদের কাছে প্রকাশ বা লগ করা হতে পারে৷
সর্বোত্তম অনুশীলন:
সংবেদনশীল ডেটা নিরাপদ হ্যান্ডলিং:
// Example: Safely handling sensitive data
function processPassword(password) {
// Create a buffer to hold the password
const passwordBuffer = Buffer.from(password);
// Process the password (e.g., hashing)
const hashedPassword = hashPassword(passwordBuffer);
// Zero out the original password buffer for security
passwordBuffer.fill(0);
return hashedPassword;
}
// Simple hashing function for demonstration
function hashPassword(buffer) {
// In a real application, you would use a cryptographic hash function
// This is a simplified example
let hash = 0;
for (let i = 0; i < buffer.length; i++) {
hash = ((hash << 5) - hash) + buffer[i];
hash |= 0; // Convert to 32-bit integer
}
return hash.toString(16);
}
// Usage
const password = 'secret123';
const hashedPassword = processPassword(password);
console.log('Hashed password:', hashedPassword);
সারাংশ
Node.js Buffer . :
- বাফারগুলি জাভাস্ক্রিপ্টে বাইনারি ডেটা ম্যানিপুলেট করার একটি উপায় প্রদান করে
- বাফার তৈরি করতে Buffer.alloc(), Buffer.from(), এবং Buffer.allocUnsafe() ব্যবহার করুন
- বাফারগুলিকে রাইট(), টোস্ট্রিং(), স্লাইস(), এবং কপি() এর মতো পদ্ধতির মাধ্যমে ব্যবহার করা যেতে পারে।
- বাফারগুলি UTF-8, বেস64 এবং হেক্স সহ বিভিন্ন ধরণের এনকোডিং সমর্থন করে
- বাফারগুলি সাধারণত ফাইল I/O, নেটওয়ার্ক অপারেশন এবং বাইনারি ডেটা প্রক্রিয়াকরণে ব্যবহৃত হয়
- বাফারগুলির সাথে কাজ করার সময় কর্মক্ষমতা এবং নিরাপত্তার প্রভাব বিবেচনা করুন